package com.asia5b.a5mall.middleware.gsonadapter;

import android.app.Application;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Converter;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2018/9/11.
 */
public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Application application;
    private final Gson gson;
    private final TypeAdapter<T> adapter;
    private final Type type;

    GsonResponseBodyConverter(Application application, Gson gson, TypeAdapter<T> adapter, Type type) {
        this.application = application;
        this.gson = gson;
        this.adapter = adapter;
        this.type = type;
    }

    /**
     * 返回数据统一处理
     *
     * @param value ResponseBody
     * @return value 返回异常
     * @throws IOException 返回数据统一处理
     */
    @Override
    public T convert(ResponseBody value) throws IOException {
        d("--->>>>>>>>---convert");
        String response = value.string();
        try {
            if (true) {
                //丢弃掉所有的null为的key(value)
                String notNullValueString = giveupNullValue(response);
                d("--->>>>>>>>---notNullValueString:" + notNullValueString);
                if (!TextUtils.isEmpty(notNullValueString)) {
                    response=notNullValueString;
                }
            }
            return gson.fromJson(response, type);
        } catch (Exception e) {
            throw e;
        } finally {
            value.close();
        }


    }

    private String giveupNullValue(String response) {
        try {

            JsonParser jsonParser = new JsonParser();
            JsonElement jsonElement = jsonParser.parse(response);
            if (jsonElement != null) {
                giveupNullValue(jsonElement);
                return jsonElement.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void giveupNullValue(JsonElement jsonElement) {
        if (jsonElement.isJsonObject()) {
            giveupNullValue((JsonObject) jsonElement);
        } else if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = (JsonArray) jsonElement;
            for (JsonElement element : jsonArray) {
                if (element != null) {
                    giveupNullValue(element);
                }
            }
        }
    }

    private void giveupNullValue(JsonObject jsonObject) {
        Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> next = iterator.next();
            String key = next.getKey();
            JsonElement value = next.getValue();
            if (value == null||TextUtils.equals(value.toString(),"null")) {
                iterator.remove();
            } else {
                giveupNullValue(value);
            }
        }
    }

}